
$(function() {
	var mySwiper1 = new Swiper('.swiper-container2', {
		direction: 'horizontal',
		loop: true,
	});
	initPage();
	initButton();
	initBottom();
});

// 初始化页面
function initPage() {
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/product/getZXList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			type : 5,
			userId:localStorage.getItem('userid')
		},
		success : function(result) {
			//console.log(result);
			if (result.retCode == '0000') {
				var str = '';
				var proList = result.data;
				if(proList.length>=1){
					$.each(proList,function(i,n){
						str +='<div class="inf-list clear teatea" productid="'+n.productid+'" type="'+n.cateid+'"><div class="inf-l fl"><p class="">'+n.teaTitle+'</p><div class="time-box clear"><span class="num-s fl">'+n.teaSellNum+'次</span><span class="time-s fr">'+n.addtime+'</span></div></div><div class="inf-r fr"><img src="'+n.pic+'" alt=""></div></div>';
					});
					
				}
				$('#tea05').html(str);
			} else{
				showErrorMsg(result.data);
			}
		}
	});
}


/**
 * 点击事件
 */
function initButton() {
	$("body").on('click','.teatea',function(){
		var param = {};
		param.productId = $(this).attr('productid');
		param.type = $(this).attr('type');
		//跳转
		localStorage.setItem('param',JSON.stringify(param));
		window.location=server_url+"infDetail.do";
	});
	//发布茶讯息
	$(".writer-box").on('click',function(){
		window.location = server_url + "releaseInf.do";
	});
	
	//搜索事件
	$('.search-con').on('click',function(){
		localStorage.setItem('oldPath',"inf.do");
		window.location = server_url + "searchResult.do";
	});
	
	
	$(".chose_bank").click(function() {
		$(this).parent().siblings('.bank_xl').show();
		var index = $(this).index();
		//alert(index);
		
	});
	$(".hotBank-list-ico").bind('click', function() {
		$(this).parents('.bank_xl').siblings('.position-rel').children('.form-control').removeAttr('placeholder');
		$(this).parents('.bank_xl').siblings('.position-rel').children('.chose_bank').children('span').html($(this).attr('title'));
		$(this).parents('.bank_xl').hide();
		search();
	});
}

function search(){
	var expressArea = $("#expressArea").html();
	//alert(expressArea);
	var fenlei = $("#fenlei").html();
	//alert(fenlei);
	var testTop =$("#testTop").val();
	//alert(testTop);
	$.ajax({
		type : "post",
		url : server_url
				+ 'rest/tea/product/getZXList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			type : 5,
			userId:localStorage.getItem('userid'),
			expressArea:expressArea,
			fenlei:fenlei,
			testTop:testTop
		},
		success : function(result) {
			//console.log(result);
			if (result.retCode == '0000') {
				var str = '';
				var proList = result.data;
				if(proList.length>=1){
					$.each(proList,function(i,n){
						str +='<div class="inf-list clear teatea" productid="'+n.productid+'" type="'+n.cateid+'"><div class="inf-l fl"><p class="">'+n.teaTitle+'</p><div class="time-box clear"><span class="num-s fl">'+n.teaSellNum+'次</span><span class="time-s fr">'+n.addtime+'</span></div></div><div class="inf-r fr"><img src="'+n.pic+'" alt=""></div></div>';
					});
					
				}
				$('#tea05').html(str);
			} else{
				showErrorMsg(result.data);
			}
		}
	});
}


function changePage(index) {
	//初始化显得是的茶人茶艺在数据库存的type为2
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/product/getMyCollection.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			type : index,
			userId:localStorage.getItem('userid')
		},
		success : function(result) {
			//alert(index);
			//console.log(result);
			if (result.retCode == '0000') {
				var str = '';
				var proList = result.data;
				if(proList.length>=1){
					//str += '<div class="recommend-box"><div class="recommend-tit">猜你喜欢</div></div><div class="pro-list-b clear">';
					$.each(proList,function(i,n){
						
						//str +='<div class="fl pro-item-b" id="'+n.productid+'" type="'+n.cateid+'"><img src="'+n.pic+'" alt=""><div class="name-box clear"><p class="fr normap">'+n.productname+'</p></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span></div><p class="fr">已售'+n.teaSellNum+'件</p></div></div>';
						//str +='<img src="'+n.pic+'" alt="" class="fl"><div class="skill-box fr"><div class="item-det clear"><span class="fl name">'+n.legalName+'</span><span class="fl color02">'+n.teaIndustry+'</span></div><div class="item-time2 clear"><span class="fl">'+n.teaSex+'/'+n.teaAge+'岁/从业'+n.teaYear+'年/'+n.teaTrip+'</span></div><div class="item-time2 clear"><span class="fl">成交量：'+n.teaSellNum+'</span><span class="fr">月薪面议</span></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span>万元</div><p class="fr">'+n.detailAddress+'</p></div></div>';

						var teaIndustryStr = ''
						if(n.teaIndustry != null && n.teaIndustry.length >0){
							var teaIndustrys = n.teaIndustry.split(",");
							$.each(teaIndustrys,function(i,n){
								if(n==0){
									teaIndustryStr += '<span class="fl color02">茶艺师</span>';
								}else if(n==1){
									teaIndustryStr += '<span class="fl color03">评茶员（师）</span>';
								}else if(n==2){
									teaIndustryStr += '<span class="fl color02">制茶师</span>';
								}else if(n==3){
									teaIndustryStr += '<span class="fl color03">茶园园艺工</span>';
								}
							});
						}
						var age = n.teaAge;
						var year = n.teaYear;
						var trip;
						if(n.teaTrip=='0'){
							trip = '不出差';
						}else{
							trip = '可出差';
						}
						var gandenTypeStr = '';
						if(n.gandenType==0){
							gandenTypeStr = "国家生态林";
						}else if(n.gandenType==1){
							gandenTypeStr = "成型茶园";
						}else if(n.gandenType==2){
							gandenTypeStr = "荒山茶林";
						}
						if(index == 0){
							//茶人茶艺
							str +='<div class="skill-list clear teatea" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'" alt="" class="fl"><div class="skill-box fr"><div class="item-det clear"><span class="fl name">'+n.productname+'</span>'+teaIndustryStr+'</div><div class="item-time clear"><span class="fl">女/'+n.teaAge+'岁/从业'+n.teaYear+'年/'+trip+'</span></div><div class="item-time clear"><span class="fl">成交量：'+n.teaSellNum+'</span><span class="fr">月薪面议</span></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span>元</div><p class="fr">'+n.teaLocation+'</p></div></div></div>';
						}else if(index == 1){
							//茶叶频道
							str += '<div class="fl pro-item teatea" productid="'+n.productid+'" type="'+n.cateid+'"><img src="'+n.pic+'" alt="" /><div class="name-box clear"><span class="fl red-box	">现购中</span><p class="fr normap">'+n.productname+'</p></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span></div><p class="fr">已售'+n.teaSellNum+'件</p></div></div></div>';
							//str += '<div class="fl pro-item"><img src="'++'" alt=""><div class="name-box clear"><span class="fl red-box">现购中</span><p class="fr normap">'+n.productname+'</p></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span></div><p class="fr">已售'+n.teaSellNum+'件</p></div></div>';
						}else if(index == 2){
							//茶区农产
							str += '<div class="fl pro-item teatea" productid="'+n.productid+'" type="'+n.cateid+'"></div><img src="'+n.pic+'" alt="" /><div class="name-box clear"><span class="fl red-box	">现购中</span><p class="fr normap">'+n.productname+'</p></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span></div><p class="fr">已售'+n.teaSellNum+'件</p></div></div></div>';
						}else if(index == 3){
							//茶园茶山
							str +=' <div class="list-item clear teatea" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'" alt="" class="fl"><div class="item-r fl"><p class="item-tit">'+n.teaTitle+'</p><div class="item-det clear"><span class="fl color01">'+gandenTypeStr+'</span><span class="fl color03">已认证</span></div><div class="item-time clear"><p class="fl">'+n.teaArea+'亩</p><span class="fr">'+n.limitTime+'年</span></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span>万元</div><p class="fr">'+n.teaLocation+'</p></div></div></div></div>';
						}
					});
					if(index == 0){
						$('#tea00').html(str);
					}else if(index == 1){
						$('#tea01').html(str);
					}else if(index == 2){
						$('#tea02').html(str);
					}else if(index == 3){
						$('#tea03').html(str);
					}
					
				}
			} else{
				showErrorMsg(result.data);
			}
		}
	});
}
//绑定下列表切换
function initBottom(){
	//搜索按钮
	$('body').on('click','.search-btn2',function(){
		search();
	});
	//跳转到首页
	$("#index").bind('click',function(){
		window.location=server_url+"buyer/product/index.do";
	});
	// 跳转到茶山市场
	$("#tea-market").bind('click', function() {
		window.location = server_url + "teaMarket.do";
	});
	//跳转到我的
	$("#tea-my").bind('click', function() {
		window.location = server_url + "teaMy.do";
	});
	//跳转到发布
	$(".demand-btn").bind('click', function() {
		window.location = server_url + "release.do";
	});
}
